package com.blunka.framework.client.ui.dialog;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;

import com.blunka.framework.client.utils.DesignUtils;
/**
 * 用户自定义配置
 * @author Administrator
 *
 */
public class UserConfigDialog extends JDialog implements ActionListener {
	private RSyntaxTextArea userArea;
	private JPanel framePanel;
	private JPanel bottomPanel;
	private JButton okButton;
	private JButton noButton;
	private String config;

	public UserConfigDialog() {
		setSize(400, 330);

		setTitle("用户自定义模式设置");

		setLayout(new BorderLayout());

		this.framePanel = new JPanel();

		this.bottomPanel = new JPanel();

		add(this.framePanel, "Center");

		add(this.bottomPanel, "South");

		this.framePanel.setLayout(new BorderLayout());

		userArea = new RSyntaxTextArea();
		userArea.setSyntaxEditingStyle(RSyntaxTextArea.SYNTAX_STYLE_JAVASCRIPT);

		//this.userArea.setFont(new Font("黑体", 1, 14));

		//this.userArea.setForeground(Color.BLUE);

		JScrollPane areaScrollpanel = new JScrollPane(this.userArea);

		this.framePanel.add(areaScrollpanel, "Center");

		Box box_button = new Box(0);
		this.bottomPanel.add(box_button);
		this.okButton = new JButton("确   定");
		this.okButton.addActionListener(this);
		box_button.add(this.okButton);

		this.noButton = new JButton("取   消");
		this.noButton.addActionListener(this);
		box_button.add(this.noButton);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.okButton) {
			String uconfig = this.userArea.getText();
			if (uconfig.indexOf("'") >= 0) {
				DesignUtils.showMessage(null, "输入的信息包含非法字符“引号”！", "系统提示", JOptionPane.WARNING_MESSAGE);
				return;
			}

			setUserConfig(uconfig);

			dispose();
		} else if (e.getSource() == this.noButton) {
			setUserConfig(getUserConfig());

			dispose();
		}
	}

	public void setUserConfig(Object config) {
		this.config = String.valueOf(config == null ? "" : config);

		this.userArea.setText(this.config);
	}

	public String getUserConfig() {
		return this.config;
	}

	protected void processWindowEvent(WindowEvent e) {
		if (e.getID() == 201) {
			setUserConfig(getUserConfig());

			dispose();
		}
	}

	public void aa(){
		RSyntaxTextArea editArea = new RSyntaxTextArea();
		AbstractTokenMakerFactory atmf = (AbstractTokenMakerFactory) TokenMakerFactory.getDefaultInstance();
		atmf.putMapping("PzcSQLTokenMaker", "pzc.gui.PzcSQLTokenMaker");
		//pzc.gui.PzcSQLTokenMaker改成PzcSQLTokenMaker.class.getName()也可以
		TokenMakerFactory.setDefaultInstance(atmf);
		editArea.setSyntaxEditingStyle("PzcSQLTokenMaker");

	}
}